package com.golfbox.oa.code;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class ZxingBarCodeUtil {
    /**
     * 根据字符串生成条形码
     * 
     * @param code 字符串
     * @return
     */
    public static BitMatrix getShapeCode(String code) {
        // 编码条形码
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");
        BitMatrix matrix = null;
        try {
            // 使用code_128格式进行编码生成100*25的条形码
            matrix = new MultiFormatWriter().encode(code,BarcodeFormat.CODE_128, 300, 100, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return matrix;
    }

    /** 获取条形码 */
    public static void getShapeCode(HttpServletRequest request, HttpServletResponse response) {
        BitMatrix matrix = getShapeCode(request.getParameter("printCode"));
        // 返回png图片流
        // 获得Servlet输出流
        ServletOutputStream outStream = null;
        try {
            outStream = response.getOutputStream();
            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
            ImageIO.write(bufferedImage, "png",outStream);
            outStream.flush();
            // 关闭输出流
            outStream.close();
        } catch (IOException e) {
            String simplename = e.getClass().getSimpleName();
            if (!"ClientAbortException".equals(simplename)) {
                e.printStackTrace();
            }
        }
    }
    //绘制文字添加间距
    public static void MyDrawString(String str,int x,int y,double rate,Graphics g){  
        String tempStr=new String();  
        int orgStringWight=g.getFontMetrics().stringWidth(str);  
        int orgStringLength=str.length();  
        int tempx=x;  
        int tempy=y;  
        while(str.length()>0)  
        {  
            tempStr=str.substring(0, 1);  
            str=str.substring(1, str.length());  
            g.drawString(tempStr, tempx, tempy);  
            tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate);  
        }  
    }  
    
    public static void main(String[] args) {
    	String code = "SKYNET201803"+new Random().nextInt(1000);
    	System.out.println(code);
        BufferedImage buff = MatrixToImageWriter.toBufferedImage(getShapeCode(code)); 
        System.out.println(buff.getType());
        BufferedImage newBuff = new BufferedImage(300,130,BufferedImage.TYPE_BYTE_BINARY);
        
        System.out.println(newBuff.getHeight());
        Graphics graphics = newBuff.getGraphics();
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.fillRect(0,0,300,150);//填充整个屏幕  
        g2d.setColor(Color.BLACK); //设置笔刷
        Font font=new Font("Courier New",Font.PLAIN,14);
        g2d.setFont(font);
        //g2d.drawString(code, 100, 120);
        MyDrawString(code, 80, 120, 1.2, g2d);
        g2d.dispose();
        newBuff.setData(buff.getData());// = buff.getSubimage(0, 0, 300, 100);
        try {
            ImageIO.write(newBuff, "png", new File("c://"+code+".png"));
            System.out.println("已生成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}